package com.wss.lsl.lucene.demo;

import static junit.framework.Assert.assertEquals;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import com.webssky.jcseg.core.JcsegTaskConfig;
import com.webssky.jcseg.lucene.JcsegAnalyzer4X;

public class AnalyzerDemo {
	public static String NAME = "魏升水 我是中国人. 密码:";
	
	public static void main(String[] args) throws IOException, ParseException {
		
		Analyzer analyzer = null;
		
		/**
		 *  StandardAnalyzer。标准分词器，中文被拆分成单个字
		 */
		System.out.println("********************** StandardAnalyzer **********************");
		analyzer = new StandardAnalyzer(Version.LUCENE_44);
		assertEquals(NAME, analyzerTest(analyzer, "魏是"));
		assertEquals(NAME, analyzerTest(analyzer, "我升"));
		System.out.println();
		
		/**
		 * CJKAnalyzer 二分法分词
		 */
		System.out.println("********************** CJKAnalyzer **********************");
		analyzer = new CJKAnalyzer(Version.LUCENE_44);
		assertEquals(NAME, analyzerTest(analyzer, "魏升"));
		assertEquals(NAME, analyzerTest(analyzer, "升水"));
		assertEquals(null, analyzerTest(analyzer, "魏是"));
		assertEquals(NAME, analyzerTest(analyzer, "魏升水"));
		assertEquals(null, analyzerTest(analyzer, "魏"));
		System.out.println();
		
		/**
		 * PaodingAnalyzer 庖丁分词器。不兼容lucene 4.4
		 */
		/*System.out.println("********************** PaodingAnalyzer **********************");
		analyzer = new PaodingAnalyzer();
		analyzerTest(analyzer, "魏升");
		analyzerTest(analyzer, "升水");
		analyzerTest(analyzer, "魏是");
		analyzerTest(analyzer, "魏升水");
		System.out.println();*/
		
		/**
		 * jcseg
		 */
		JcsegAnalyzer4X analyzer4x = new JcsegAnalyzer4X(JcsegTaskConfig.COMPLEX_MODE);
		JcsegTaskConfig config = analyzer4x.getTaskConfig();
		//追加同义词到分词结果中, 需要在 jcseg.properties 中配置 jcseg.loadsyn=1
		//追加拼音到分词结果中, 需要在 jcseg.properties 中配置 jcseg.loadpinyin=1
		config.setAppendCJKSyn(true); 
		config.setAppendCJKPinyin(true);
		System.out.println("********************** JcsegAnalyzer4X **********************");
		analyzer = new CJKAnalyzer(Version.LUCENE_44);
		analyzerTest(analyzer4x, "魏升");
		analyzerTest(analyzer4x, "升水");
		analyzerTest(analyzer4x, "魏是");
		analyzerTest(analyzer4x, "魏升水");
		analyzerTest(analyzer4x, "魏");
		analyzerTest(analyzer4x, "密码");
		assertEquals(NAME, analyzerTest(analyzer4x, "魏升"));
		assertEquals(NAME, analyzerTest(analyzer4x, "升水"));
		assertEquals(NAME, analyzerTest(analyzer4x, "魏是"));
		assertEquals(NAME, analyzerTest(analyzer4x, "魏升水"));
		assertEquals(NAME, analyzerTest(analyzer4x, "魏"));
		assertEquals(NAME, analyzerTest(analyzer4x, "密码"));
		assertEquals(null, analyzerTest(analyzer4x, "密"));
		assertEquals(null, analyzerTest(analyzer4x, "人"));
		assertEquals(NAME, analyzerTest(analyzer4x, "中国人"));
		System.out.println();
		
		analyzer.close();
		analyzer4x.close();
		
		System.out.println("Done!");
	}
	
	
	public static String analyzerTest(Analyzer analyzer, String queryStr) throws IOException, ParseException{
		// index
		Directory directory = new RAMDirectory();
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
		IndexWriter writer = new IndexWriter(directory, config);
		Document doc = new Document();
		doc.add(new TextField("name", NAME, Field.Store.YES));
		doc.add(new StringField("age", "25", Field.Store.YES));
		writer.addDocument(doc);
		writer.close();
		
		IndexReader reader = DirectoryReader.open(directory);
		IndexSearcher searcher = new IndexSearcher(reader);
		Query query = new QueryParser(Version.LUCENE_44, "name", analyzer).parse(queryStr);
		ScoreDoc[] hits = searcher.search(query, 10).scoreDocs;
		for(ScoreDoc hit : hits){
			Document document = searcher.doc(hit.doc);
			// System.out.println("queryStr: "+ queryStr + "\tname: " + document.get("name"));
			return document.get("name");
		}
		reader.close();
		return null;
		
	}
	
}
